<?php

namespace App\Http\Controllers\Admin\OrderManager;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

//提现白名单
class WithdrawWhitelistController extends BaseController
{
    //新增提现白名单
    public function add(Request $request)
    {
        $param = $request->only('account', 'user_code', 'name', 'id_card_no', 'type', 'bank_name', 'bank_zhi');
        $v = app('validator')->make($param, [
            'account'    => 'required|string',
            'user_code'  => 'required|integer',
            'name'       => 'required|string|max:11',
            'id_card_no' => 'string|max:18',
            'bank_name' => 'string|max:18',
            'type'       => 'integer|in:1,2',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        app('repo_withdraw_whitelist')->addWithdrawWhitelist($param);

        return Response::sendData([]);
    }

    //获取账户信息
    public function getAlipayAccountInfo(Request $request)
    {
        $param = $request->only('account', 'type');
        $v = app('validator')->make($param, [
            'account' => 'required|string',
            'type'    => 'integer|in:1,2,3,4',//1支付宝，2微信，3银行卡，4对公账号
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        $type = $param['type'] ?? 1;
        $data = app('repo_withdraw_whitelist')->getAlipayAccountInfo($param['account'], $type);

        return Response::sendData($data);
    }

    //移除提现白名单
    public function del(Request $request)
    {
        $param = $request->only('ids');
        $v = app('validator')->make($param, [
            'ids' => 'required|string',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        app('repo_withdraw_whitelist')->delWithdrawWhitelist($param['ids']);

        return Response::sendData([]);
    }

    //提现白名单列表
    public function list(Request $request)
    {
        $param = $request->only('page', 'per_page', 'type', 'user_code', 'nickname', 'mobile', 'account');
        $v = app('validator')->make($param, [
            'page'     => 'integer|min:1',
            'per_page' => 'integer|min:1|max:100',
            'type' => 'integer|in:1,2',
        ]);
        if ($v->fails()) {
            throw new BasicException(0, $v->errors()->first());
        }

        $list = app('repo_withdraw_whitelist')->withdrawWhitelist($param);

        return Response::sendData($list);
    }
}
